class AppliesController < ApplicationController

  before_action :required_current_member

  before_action :set_apply, only: [:show, :edit, :update, :destroy,:reply,:execute_reply]
  before_action :set_doc, :only => [:new]

  # GET /applies
  # GET /applies.json
  def index
    @applies = initialize_grid(current_project.applies,:order=>"id")
  end

  # GET /applies/1
  # GET /applies/1.json
  def show
  end

  # GET /applies/new
  def new
    if current_member.can_read_doc(@doc)
      #return redirect_to request.referer || root_path, :notice=>'您可以直接下载，预览该文件无需借阅'
    end 
    @apply = Apply.new(:doc_id=>@doc.id,:user_id=>current_user.id)
  end

  # GET /applies/1/edit
  def edit
    unless current_member.can_control_doc @apply.doc
      return  redirect_to request.referer || root_path, :alert=>'您没有权限编辑借阅申请'
    end
  end

  # POST /applies
  # POST /applies.json
  def create
    @apply = Apply.new(apply_params.merge(:user_id=>current_user.id,:project_id=>current_project.id))

    respond_to do |format|
      if @apply.save
        format.html { redirect_to applies_doc_path(@apply.doc), notice: '借阅申请创建成功' }
        format.json { render :show, status: :created, location: @apply }
      else
        format.html { render :new }
        format.json { render json: @apply.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /applies/1
  # PATCH/PUT /applies/1.json
  def update
    unless current_member.can_control_doc @apply.doc
      return  redirect_to request.referer || root_path, :alert=>'您没有权限修改借阅申请'
    end

    reply_params = params.require(:apply).permit(:reply_passed,:reply_remak).merge(:replied_at=>Time.now,:reply_user_id=>current_user.id)

    respond_to do |format|
      if @apply.update(reply_params)
        format.html { redirect_to applies_doc_path(@apply.doc), notice: '借阅申请修改成功' }
        format.json { render :show, status: :ok, location: @apply }
      else
        format.html { render :edit }
        format.json { render json: @apply.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /applies/1
  # DELETE /applies/1.json
  def destroy
    unless current_member.can_control_doc(@apply.doc)
      return  redirect_to request.referer || root_path, :alert=>'您没有权限删除借阅申请'
    end
    path = applies_doc_url(@apply.doc_id)
    @apply.destroy
    respond_to do |format|
      format.html { redirect_to path }
      format.json { head :no_content }
    end
  end

  def reply
    
  end

  def execute_reply
    reply_params = params.require(:apply).permit(:reply_passed,:reply_remak).merge(:replied_at=>Time.now,:reply_user_id=>current_user.id)
    #
    respond_to do |format|
      if @apply.update(reply_params)
          format.html { redirect_to @apply, notice: 'Apply was successfully updated.' }
          format.json { render :show, status: :ok, location: @apply }
      else
          format.html { render :edit }
          format.json { render json: @apply.errors, status: :unprocessable_entity }
      end
    end
  end

 

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_apply
      @apply = Apply.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def apply_params
      params.require(:apply).permit(:doc_id,:remark)
    end

    def set_doc
       @doc = current_project.docs.find_by_id(params[:doc_id])
       unless @doc
         redirect_to request.referer || root_path, alert: '请选择您要借阅的文件'
       end
    end
end
